/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;
import org.jbox2d.collision.shapes.PolygonDef;
import org.jbox2d.common.Vec2D;


public class Seat extends Box{
    public Seat() throws WorldsException
    {
        super("Seat");
        addPropChangeHandler("Size", new PropChangeHandler()
        {
            @Override
            public void handle(final Object oldValue, final Object value) throws WorldsException {

                try {
                    Vec2D vec = (Vec2D) value;
                    ((PolygonDef) sd).setAsBox((float) vec.x / 2, (float) vec.y / 2);
                    if(inWorld)
                        rebuildShape();
                    size = (Vec2D) value;
                } catch (WorldsException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        addPropChangeHandler("Position", new PropChangeHandler()
        {
            @Override
            public void handle(final Object oldValue, final Object value) throws WorldsException {
                if(inWorld)
                {
                    Vec2D pos = (Vec2D) value;
                    body.setXForm(pos, body.getAngle());
                    body.wakeUp();
                    wakeUpConnected();
                }
                else
                    bd.position = ((Vec2D)value);
            }
        });
    }
    @Override
    public void destructor() throws WorldsException
    {
        super.destructor();
        if(Main.getLocalPlayer() != null && Main.getLocalPlayer().getCharacter() != null && Main.getLocalPlayer().getCharacter().seat == this)
            Main.getLocalPlayer().getCharacter().setSeat(null);
    }
}
